﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Reflection;
using System.Threading;

/*
 * listBox4 Urgent 记录在 weeks.txt 中
 * 
 * 从 vs 的 源代码服务 更换到 码云上
 * 20171130 设置 save_all 替换 5min的保存和推出时保存的 两段代码（合并成一个函数）
 */

/*还有这些bug没有修改好
 * 1. listbox 选中的时候，再去勾选 mofidy 会出发 无引用的对象
 * 2. richText 能够直接通过 修改添加进来，不合适 （如果这里加进来的 没有 & 会导致后续分割 超出数组界限）


关于任务性质定义：
0 表示5min即时任务

1 表示小时任务-工作必需
2 表示小时任务-工作可需

3 表示小时任务-自我必须
4 表示小时任务-自我可需

// 长期任务是一段时间内，总要被执行，但无法完成的（一般都为 重要不紧急的任务）
9 表示长期任务-领导要求
8 表示长期任务-自我核心
7 表示长期任务-自我周边
6 表示长期任务-工作核心
5 表示长期任务-工作周边

 */

/* 修改按钮 的添加方式
 *             ListBox listBox = listBox3;
 *             modify_click(listBox);
 * 让文件可以修改 index_change
 *             ListBox listBox = listBox3;
 *             index_change(listBox);
*/

namespace FoxStart
{
    public partial class Form1 : Form
    {
        string path;
        bool Timekey = false;
        int timemodify = 1; // min

        public static int woshibushijinglaiguo = 0;//通过这个控制 只有 配置界面 点击确认才会进入后续代码

        string timew;
        string times;
        //因为在 gettotxt() 中直接访问，有其他线程更改的 label9 label10 的Text 是空的
        //设置这两个全局变量，转接一下就好了

        //bool ModfListB1 = false;

        //给 check_click 用的
        //附加形式的，每次都是添加 每次写完要 .Flush();程序关闭时，释放文件占用
        static FileStream file_ = new FileStream("delete.log", FileMode.Append);
        StreamWriter file_log = new StreamWriter(file_);

        //给 today / 收集箱 用的
        //附加形式的，每次都是添加 每次写完要 .Flush();程序关闭时，释放文件占用
        static FileStream file_1 = new FileStream("NoUrgent.log", FileMode.Append);
        StreamWriter file_NoUrgent = new StreamWriter(file_1);

        bool canin = true;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //Undo.txt

            Form1.CheckForIllegalCrossThreadCalls = false; // 这句话很重要，(跨线程)访问

            timer1.Enabled = false;
            Form2 f;
            f = new Form2();
            f.ShowDialog();

            if (woshibushijinglaiguo == 0)
            {
                System.Environment.Exit(0);
            }

            int TT = 60 * 1000 * f.GetChangeValue();

            //20170921 改造 添加 Undo.txt
            try
            {
                //Weeks.txt的读取
                StreamReader sr0 = new StreamReader("Weeks.txt", Encoding.GetEncoding("UTF-8"));
                String line0;
                while ((line0 = sr0.ReadLine()) != null)
                {
                    if (line0.Split('$')[0] == "[Urgent]")
                    {
                        ListBox list = this.listBox4;
                        list.Items.Add(line0.Split('$')[1]);
                    }
                    else if (line0.Split('$')[0] == "[week]")
                    {
                        ListBox list = this.checkedListBox3;
                        list.Items.Add(line0.Split('$')[1]);
                    }
                    else if (line0.Split('$')[0] == "[month]")
                    {
                        ListBox list = this.checkedListBox4;
                        list.Items.Add(line0.Split('$')[1]);
                    }
                    else if (line0.Split('$')[0] == "[today]")
                    {
                        ListBox list = this.listBox3;
                        list.Items.Add(line0.Split('$')[1]);
                    }
                }
                sr0.Close();

                StreamReader sr = new StreamReader("Undo.txt", Encoding.GetEncoding("UTF-8"));
                String line;
                int Ww = 0;
                while ((line = sr.ReadLine()) != null)
                {
                    if (line == "======这是Work未完成事项======")
                    {
                        Ww = 1;
                        continue;
                    }
                    else if (line == "======这是Self未完成事项======")
                    {
                        Ww = 3;
                        continue;
                    }
                    else
                    {
                        if (Ww == 1)
                        {
                            checkedListBox1.Items.Add(line);
                        }
                        else if (Ww == 3)
                        {
                            checkedListBox2.Items.Add(line);
                        }
                        else
                        {

                        }
                    }
                    //Console.WriteLine(line.ToString());
                }
                sr.Close();
            }
            catch
            {

            }


            path = f.GetPath();
            if (path.Length > 0)
            {
                //StreamReader sr = new StreamReader(path, Encoding.Default);
                StreamReader sr = new StreamReader(path, Encoding.GetEncoding("UTF-8"));
                String line;
                int Ww = 0;
                while ((line = sr.ReadLine()) != null)
                {
                    if (line.Split('&')[0] == "======这是Work未完成事项======")
                    {
                        Ww = 1;
                        continue;
                    }
                    else if (line.Split('&')[0] == "======这是Work已完成事项======")
                    {
                        Ww = 0;
                        continue;
                    }
                    else if (line.Split('&')[0] == "======这是Self未完成事项======")
                    {
                        Ww = 3;
                        continue;
                    }
                    else if (line.Split('&')[0] == "======这是Self已完成事项======")
                    {
                        Ww = 3;
                        continue;
                    }
                    else
                    {
                        if (Ww == 1)
                        {
                            //checkedListBox1.Items.Add(line); //由 Undo.txt 处理
                        }
                        else if (Ww == 3)
                        {
                            continue;
                        }
                        else
                        {
                            listBox1.Items.Add(line);
                        }
                    }
                    //Console.WriteLine(line.ToString());
                }
                sr.Close();
            }

            if (path.Length > 0)
            {
                //StreamReader sr = new StreamReader(path, Encoding.Default);
                StreamReader sr = new StreamReader(path, Encoding.GetEncoding("UTF-8"));
                String line;
                int Ww = 0;
                while ((line = sr.ReadLine()) != null)
                {
                    if (line.Split('&')[0] == "======这是Self未完成事项======")
                    {
                        Ww = 1;
                        continue;
                    }
                    else if (line.Split('&')[0] == "======这是Work未完成事项======")
                    {
                        Ww = 3;
                        continue;
                    }
                    else if (line.Split('&')[0] == "======这是Work已完成事项======")
                    {
                        Ww = 3;
                        continue;
                    }
                    else if (line.Split('&')[0] == "======这是Self已完成事项======")
                    {
                        Ww = 0;
                        continue;
                    }
                    else
                    {
                        if (Ww == 1)
                        {
                            // checkedListBox2.Items.Add(line);//由 Undo.txt 处理
                        }
                        else if (Ww == 3)
                        {
                            continue;
                        }
                        else
                        {
                            listBox2.Items.Add(line);
                        }
                    }
                    //Console.WriteLine(line.ToString());
                }
                sr.Close();
            }

            //开始自动保存线程
            Thread o1Thread = new Thread(() => with5min());
            //Woshiyigejishiqi(time, pob, nowstring);
            o1Thread.Start();

            timer1.Interval = TT;
            label5.Text = (TT / 60 / 1000).ToString() + " Min";
            timer2.Interval = 1000;
            timer1.Enabled = true;

        }

        private void timer1_Tick(object sender, EventArgs e)
        {//20180108去掉显示桌面，影响工作
            // timer1.Enabled = false;
            // Type oleType = Type.GetTypeFromProgID("Shell.Application");
            // object oleObject = System.Activator.CreateInstance(oleType);
            // oleType.InvokeMember("ToggleDesktop", BindingFlags.InvokeMethod, null, oleObject, null);//报错地方自行引用空间名
            // //MessageBox.Show("Time Get ...");
            // Form3 f;
            // f = new Form3();
            // f.ShowDialog();
            // timer1.Enabled = true;
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            string[] sArray = DateTime.Now.ToString().Split(':');
            label1.Text = sArray[0] + ":" + sArray[1];
            all_time(sender, e);
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void chart1_Click(object sender, EventArgs e)
        {

        }

        private void fontDialog1_Apply(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void label5_Click(object sender, EventArgs e)
        {

        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {

        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            string Text = this.richTextBox1.Text.Trim().ToString();
            ListBox list = this.checkedListBox1;
            string[] sArray = DateTime.Now.ToString().Split(':');
            list.Items.Add(Text + " " + sArray[0] + ":" + sArray[1]);
            richTextBox1.Clear();
            can1in = true;
            checkBox1.Checked = false;
        }

        private void maskedTextBox1_MaskInputRejected(object sender, MaskInputRejectedEventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            foreach (string item in checkedListBox1.CheckedItems)
            {
                //string[] sArray = DateTime.Now.ToString().Split(':');
                //listBox1.Items.Add("0&"+item.ToString() + " " + sArray[0] + ":" + sArray[1]);

                if (item.ToString().Split('&').Length > 1)
                {
                    listBox1.Items.Add(item.ToString());
                }
                else
                {
                    listBox1.Items.Add("300&" + item.ToString());
                }

                //自己写函数刷新感太强
                //{
                //    //label1.Text = (d);
                //    this.listBox1.DrawMode = DrawMode.OwnerDrawFixed;
                //    this.listBox1.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.listBox1_DrawItem);
                //}

                checkedListBox1.Items.Remove(item);
                break;
            }
        }

        private void checkedListBox1_ItemCheck(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged_1(object sender, EventArgs e)
        {

        }

        private void timer3_Tick(object sender, EventArgs e)
        {

        }

        private void button2_Click_1(object sender, EventArgs e)
        {
            gettotxt();
            //with5min();
        }

        private void button2_MouseEnter(object sender, EventArgs e)

        {

            bool _isBtnCutoutToolTip = false; //在MouseEnter事件里做一个判断就好了， 

            if (_isBtnCutoutToolTip) return; //_isBtnCutoutToolTip在第一次tooltip.SetToolTip()设置之后赋值为true即可。这样就不会重复设置提示框了导致多提示框了

            ToolTip p = new ToolTip();

            p.ShowAlways = true;

            p.SetToolTip(this.button2, "直接关闭就可以");

            _isBtnCutoutToolTip = true;

        }

        public void with5min()
        {
            int min = 5;
            while (true)
            {
                //string puG="G:\\ak" + DateTime.Now.ToString("D") + ".txt";
                string puG = DateTime.Now.ToString("yyyy-MM-dd") + "_temp.txt";

                save_all(puG);

                //System.Diagnostics.Process.Start(puG);
                Thread.Sleep(1000 * 60 * min);
            }

        }

        public bool backup()
        {
            string cmdExe = "";
            string[] cmdStrs = { "mkdir back", "move Undo.txt back/", "move Weeks.txt back/" };
            bool result = false;
            foreach (string cmdStr in cmdStrs)
            {
                try
                {
                    using (System.Diagnostics.Process myPro = new System.Diagnostics.Process())
                    {
                        myPro.StartInfo.FileName = "cmd.exe";
                        myPro.StartInfo.UseShellExecute = false;
                        myPro.StartInfo.RedirectStandardInput = true;
                        myPro.StartInfo.RedirectStandardOutput = true;
                        myPro.StartInfo.RedirectStandardError = true;
                        myPro.StartInfo.CreateNoWindow = true;
                        myPro.Start();
                        //如果调用程序路径中有空格时，cmd命令执行失败，可以用双引号括起来 ，在这里两个引号表示一个引号（转义）
                        string str = string.Format(@"""{0}"" {1} {2}", cmdExe, cmdStr, "&exit");

                        myPro.StandardInput.WriteLine(str);
                        myPro.StandardInput.AutoFlush = true;
                        myPro.WaitForExit();

                        result = true;
                    }
                }
                catch (Exception e)
                {
                    label1.Text = "will Error";
                    label1.Text = e.ToString(); //函数需要是 public ，不然这里不能访问，应该和 label1 的属性有关 或者 和 c# 对象特定有关
                }
            }
            return result;
        }

        public void save_all(string puG)
        {
            FileStream fs1 = new FileStream("Undo.txt", FileMode.Create);
            StreamWriter sw1 = new StreamWriter(fs1);

            //保存 checkedListBox3 checkedListBox4 到 Weeks.txt
            FileStream fs2 = new FileStream("Weeks.txt", FileMode.Create);
            StreamWriter sw2 = new StreamWriter(fs2);

            foreach (string item in listBox4.Items)
            {
                sw2.WriteLine("[Urgent]$" + item.ToString());
                //也可以使用fs 用byte的方式写入，但是不好换行
            }

            foreach (string item in checkedListBox3.Items)
            {
                sw2.WriteLine("[week]$" + item.ToString());
                //也可以使用fs 用byte的方式写入，但是不好换行
            }

            foreach (string item in checkedListBox4.Items)
            {
                sw2.WriteLine("[month]$" + item.ToString());
                //也可以使用fs 用byte的方式写入，但是不好换行
            }

            foreach (string item in listBox3.Items)
            {
                sw2.WriteLine("[today]$" + item.ToString());
                //也可以使用fs 用byte的方式写入，但是不好换行
            }

            sw2.Flush();
            sw2.Close();

            FileStream fs = new FileStream(puG, FileMode.Create);
            StreamWriter sw = new StreamWriter(fs);

            sw1.WriteLine("======这是Work未完成事项======");
            foreach (string item in checkedListBox1.Items)
            {
                sw1.WriteLine(item.ToString());
                //也可以使用fs 用byte的方式写入，但是不好换行
            }
            sw.WriteLine("======这是Work已完成事项======&" + timew);
            foreach (string item in listBox1.Items)
            {
                sw.WriteLine(item.ToString());
                //也可以使用fs 用byte的方式写入，但是不好换行
            }
            sw1.WriteLine("======这是Self未完成事项======");
            foreach (string item in checkedListBox2.Items)
            {
                sw1.WriteLine(item.ToString());
                //也可以使用fs 用byte的方式写入，但是不好换行
            }
            sw.WriteLine("======这是Self已完成事项======&" + times);
            foreach (string item in listBox2.Items)
            {
                sw.WriteLine(item.ToString());
                //也可以使用fs 用byte的方式写入，但是不好换行
            }
            sw.Flush();
            sw.Close();

            sw1.Flush();
            sw1.Close();
        }


        public void gettotxt()
        {
            //string puG="G:\\ak" + DateTime.Now.ToString("D") + ".txt";
            //更换成退出打开temp 更加便捷 以及 做修改 //string puG = "Exit_" + DateTime.Now.ToLocalTime().ToString().Replace(":", "_").Replace(" ", "#").Replace("/", "_") + ".txt";
            string puG = DateTime.Now.ToString("yyyy-MM-dd") + "_temp.txt";
            save_all(puG);

            System.Diagnostics.Process.Start(puG);
            Application.Exit();
        }


        private void label6_Click(object sender, EventArgs e)
        {

        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {

        }

        bool can1in = true;

        private void listBox1_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            ListBox listBox = listBox1;
            index_change(listBox);
        }

        public void Woshiyigejishiqi(int nowtips, string nowstring, string stringP, ListBox listBox1)
        {   // stringP 的话，是 tips + string

            if (!canin) //避免其他线程进入

            /*
         跟多线程进入没关系，是前一个还没有退出导致的问题   

        真是原因是：同时有五个线程进来是什么鬼，在没退出的时候(sleep的时候)，又进来了其他线程

         导致前面的线程不再会退出了。

        换成线程外的直接停止呢？

         */
            {
                return;
            }
            canin = false;
            int newtips = nowtips;
            while (Timekey && !checkBox1.Checked)
            {
                //修复问题 -> 处理重复添加的问题
                //stringP = (newtips.ToString() + "&" + nowstring); //不是因为这个问题，是因为多个函数进入的问题，添加控制，只让一个函数进入就可以

                // 光标移走后，没有选中的，第二次循环过来就会报错-> 放到第一次调用这个函数之前的位置
                //label7.Text = listBox1.SelectedItem.ToString();//标识现在开始记时的item

                Thread.Sleep(1000 * 1 * timemodify);
                //Thread.SpinWait(1000*60* timemodify); 不行不行，严重不符合预期
                newtips += 1;
                listBox1.Items.Remove(stringP);
                //int nIndex = listBox1.FindString(stringP);
                stringP = (newtips.ToString() + "&" + nowstring);

                //要修改才能换算，不合适不合适，这里直接改吧
                stringP = sstoflag(stringP);

                listBox1.Items.Add(stringP);
                //---//20170908//  try
                //---//20170908//  {
                //---//20170908//      listBox1.Items[nIndex] = stringP;
                //---//20170908//  }
                //---//20170908//  catch
                //---//20170908//  {
                //---//20170908//      label2.Text = stringP;
                //---//20170908//      //listBox1.Items[nIndex] = stringP;
                //---//20170908//      //吃掉最后一次刷新Ithems的错误
                //---//20170908//  }

                if (Timekey)
                {
                    if (nowstring.Length > 39)
                    {
                        label7.Text = nowstring.Substring(0, 40);
                    }
                    else
                    {
                        label7.Text = nowstring;
                    }
                }

                //这是ClistBox的用法 参考链接 修改listbox里某一行的文字_百度知道  https://zhidao.baidu.com/question/174404737.html
                //int nIndex = listbox.FindString搜索(-1, _T("待修改字符串"));
                //listbox.DeleteString(nIndex);
                //listbox.InsertString(nIndex, _T("修改后字符串"));
            }

            canin = true;
            //这个处理时很ok的
            //从 if (Timekey) 里面移过来，期望解决重复添加，多个线程一起的情况

            //label5.Text = nowtips.ToString();
        }

        //和Woshiyigejishiqi合并 public void Woshiyigejishiqi2(int nowtips, string nowstring, string stringP)
        // {   
        //
        //     if (!canin) 
        //     {
        //         return;
        //     }
        //     canin = false;
        //     int newtips = nowtips;
        //     while (Timekey && !checkBox1.Checked)
        //     {
        //         Thread.Sleep(1000 * 1 * timemodify);
        //         newtips += 1;
        //         listBox2.Items.Remove(stringP);
        //         stringP = (newtips.ToString() + "&" + nowstring);
        //
        //         //要修改才能换算，不合适不合适，这里直接改吧
        //         stringP = sstoflag(stringP);
        //
        //         listBox2.Items.Add(stringP);
        //         
        //         if (Timekey)
        //         {
        //             if (nowstring.Length > 39)
        //             {
        //                 label7.Text = nowstring.Substring(0, 40);
        //             }
        //             else
        //             {
        //                 label7.Text = nowstring;
        //             }
        //         }
        //     }
        //
        //     canin = true;
        // }

        private void click_time_add(ListBox listBox1)
        {
            string nowstring = listBox1.SelectedItem.ToString();//"我就是个计时的";
            int time = Int32.Parse(nowstring.Split('&')[0]);
            string pob = nowstring.Split('&')[1];
            // 这里肯定是没问题的，因为我们肯定是单线程处理问题的，不应该同时处理两个事项
            //不得已 可以切换，但是不会有并行
            if (!checkBox1.Checked && !checkBox2.Checked && !checkBox3.Checked) //后两个and 上去的条件，是为了解决：listbox3 → listbox4 之后，timeadd 会有把那个加到 listbox3 中间

            {
                if (Timekey)
                {
                    Timekey = false;
                    label7.Text = "No with doing";//标识现在开始记时的item

                }
                else
                {
                    Timekey = true;
                    if (listBox1.SelectedItem.ToString().Length > 39)
                    {
                        label7.Text = listBox1.SelectedItem.ToString().Substring(0, 40);//标识现在开始记时的item
                    }//上面这个语句不能放到 函数 Woshiyigejishiqi 里面去，因为可能出现 无 SelectedItem 的情况
                    else
                    {
                        label7.Text = listBox1.SelectedItem.ToString();
                    }
                    Thread oThread = new Thread(() => Woshiyigejishiqi(time, pob, nowstring, listBox1));
                    //Woshiyigejishiqi(time, pob, nowstring);
                    oThread.Start();

                    // Loop until worker thread activates.
                    //while (!oThread.IsAlive) ;

                }
            }
        }

        private void listBox1_Click(object sender, EventArgs e)
        {
            if (listBox1.Items.Count < 1 || listBox1.SelectedIndex == -1)
            {
                return;
            }
            click_time_add(listBox1);
        }

        private void listBox2_Click(object sender, EventArgs e)
        {
            if (listBox2.Items.Count < 1 || listBox2.SelectedIndex == -1)
            {
                return;
            }
            click_time_add(listBox2); ;
        }

        private void listBox3_Click(object sender, EventArgs e)
        {
            if (listBox3.Items.Count < 1 || listBox3.SelectedIndex == -1)
            {
                return;
            }
            click_time_add(listBox3); 
            today_change(listBox3);//判断是非曲直（判断要不要处理，函数入口处有判断，这里只管进去就行
        }

        private void listBox4_Click(object sender, EventArgs e)
        {
            if (listBox4.Items.Count < 1 || listBox4.SelectedIndex == -1)
            {
                return;
            }
            click_time_add(listBox4); ;
        }

        private void label8_Click(object sender, EventArgs e)
        {

        }

        private string sstoflag(string ss)
        {
            if (-1 == ss.IndexOf('&'))
            {
                ss = 299.ToString() + '&' + ss;
            }
            string pp_1 = ss.Split('&')[0];
            int pp_1_2, pp_1_1;
            string pp_1_1_tmp;
            //添加减法处理
            pp_1_1_tmp = (pp_1.Split('+')[0]);
            if (pp_1.Split('+').Length > 1)
            {
                pp_1_1 = Int32.Parse(pp_1.Split('+')[0]);
                pp_1_2 = Int32.Parse(pp_1.Split('+')[1]);
            }
            else if (pp_1.Split('-').Length > 1)
            {
                pp_1_1 = Int32.Parse(pp_1.Split('-')[0]);
                pp_1_2 = Int32.Parse(pp_1.Split('-')[1]);
                pp_1_2 = 0 - pp_1_2;
            }
            else
            {
                pp_1_1 = Int32.Parse(pp_1.Split('+')[0]);
                pp_1_2 = 0;
            }
            int time_end = pp_1_1 + pp_1_2;

            int time = time_end;
            int time1 = time / 3600;
            int time2 = (time % 3600) / 60 + 1;
            string pob = ss.Split('&')[1];

            int ww_1 = pob.IndexOf("]");
            if (ww_1 > 0)
            {
                pob = pob.Substring(ww_1 + 1);
            }

            string sflag = "[" + time1.ToString() + "h" + time2.ToString() + "min" + "]";
            string flag = time + "&" + sflag + pob;

            return flag;
        }

        private void all_time(object sender, EventArgs e)
        {
            int lab9_i = 0;
            int lab10_i = 0;

            foreach (string item in listBox1.Items)
            {
                string ss = (item.ToString());
                //也可以使用fs 用byte的方式写入，但是不好换行

                int time = Int32.Parse(ss.Split('&')[0]);
                lab9_i += time;
            }

            int time1 = lab9_i / 3600;
            int time2 = (lab9_i % 3600) / 60 + 1;
            label9.Text = "[" + time1.ToString() + "h" + time2.ToString() + "min" + "]";
            timew = label9.Text;

            foreach (string item in listBox2.Items)
            {
                string ss = (item.ToString());
                //也可以使用fs 用byte的方式写入，但是不好换行

                int time = Int32.Parse(ss.Split('&')[0]);
                lab10_i += time;
            }

            time1 = lab10_i / 3600;
            time2 = (lab10_i % 3600) / 60 + 1;
            label10.Text = "[" + time1.ToString() + "h" + time2.ToString() + "min" + "]";
            times = label10.Text;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            ListBox listBox = listBox1;
            modify_click(listBox);
        }

        private void listBox2_DrawItem(object sender, DrawItemEventArgs e)
        {
            if (e.Index < 0) //哇，这个地方如果是 < 1 的话，会影响 listBox2 的显示（实际有值，但是显示为空白）
            {
                return;
            }
            string s = this.listBox2.Items[e.Index].ToString();

            //string s = this.richTextBox1.Text.Trim().ToString();
            if (s.Contains("05."))
            {
                e.Graphics.DrawString(s, this.Font, Brushes.Red, e.Bounds);
            }
            else if (s.Contains("15."))
            {
                e.Graphics.DrawString(s, this.Font, Brushes.Green, e.Bounds);
            }
            else if (s.Contains("25."))
            {
                e.Graphics.DrawString(s, this.Font, Brushes.Blue, e.Bounds);
            }
            else
                e.Graphics.DrawString(s, this.Font, new SolidBrush(this.ForeColor), e.Bounds);
        }

        private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
        {
            if (e.Index < 0) //哇，这个地方如果是 < 1 的话，会影响 listBox2 的显示（实际有值，但是显示为空白）
            {
                return;
            }
            string s = this.listBox1.Items[e.Index].ToString();

            //string s = this.richTextBox1.Text.Trim().ToString();


            if (s.Contains("01."))
            {
                e.Graphics.DrawString(s, this.Font, Brushes.Red, e.Bounds);
            }
            else if (s.Contains("11."))
            {
                e.Graphics.DrawString(s, this.Font, Brushes.Green, e.Bounds);
            }
            else if (s.Contains("21."))
            {
                e.Graphics.DrawString(s, this.Font, Brushes.Blue, e.Bounds);
            }
            else
                e.Graphics.DrawString(s, this.Font, new SolidBrush(this.ForeColor), e.Bounds);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            string Text = this.richTextBox1.Text.Trim().ToString();
            ListBox list = this.checkedListBox2;
            string[] sArray = DateTime.Now.ToString().Split(':');

            //=====> 

            // string tmp01 = (Text.Split('.')[0]);
            // string tmp = tmp01.Substring(tmp01.Length-2,2);
            // label5.Text = tmp;
            //

            //<======

            list.Items.Add(Text + " " + sArray[0] + ":" + sArray[1]);

            richTextBox1.Clear();
            can1in = true;
            checkBox1.Checked = false;
        }

        private void checkedListBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            foreach (string item in checkedListBox2.CheckedItems)
            {
                //string[] sArray = DateTime.Now.ToString().Split(':');
                //listBox2.Items.Add("0&" + item.ToString() + " " + sArray[0] + ":" + sArray[1]);

                if (item.ToString().Split('&').Length > 1)
                {
                    listBox2.Items.Add(item.ToString());
                }
                else
                {
                    listBox2.Items.Add("300&" + item.ToString());

                }
                // 自己写函数，刷新感太强
                //{
                //    //label1.Text = (d);
                //    this.listBox2.DrawMode = DrawMode.OwnerDrawFixed;
                //    this.listBox2.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.listBox2_DrawItem);
                //}

                checkedListBox2.Items.Remove(item);
                break;
            }
        }

        private void index_change(ListBox listBox)
        {
            if (checkBox1.Checked && can1in)//&& ! Timekey
            {
                //添加一个变量，防止第二次进来 -- 在 修改 按钮 里设置为 true
                can1in = false;

                //checkBox1.Checked = false; //让 修改 按钮，自己取消，不在这里取消，不然 Woshiyigejishiqi 线程会自己放出来。

                //string tmp1 = listBox1.SelectedItem.ToString();

                //sstoflag → 添加时间换算功能
                richTextBox1.Text = sstoflag(listBox.SelectedItem.ToString());

                //Thread.Sleep(1000 * 1 * timemodify);
                if (listBox.SelectedItem.ToString().Replace(" ", "").Length > 0) //避免没选中的时候，删了不应该删除的
                {
                    listBox.Items.Remove(listBox.SelectedItem);
                }
                //listBox1.Items.Remove(richTextBox1.Text);

                //ModfListB1 = true;

            }
        }

        private void index_change_check(CheckedListBox checkedListBox)
        {
            if (checkBox1.Checked && can1in)//&& ! Timekey
            {
                //添加一个变量，防止第二次进来 -- 在 修改 按钮 里设置为 true
                can1in = false;

                //sstoflag → 添加时间换算功能
                richTextBox1.Text = sstoflag(checkedListBox.SelectedItem.ToString());

                if (checkedListBox.SelectedItem.ToString().Replace(" ", "").Length > 0) //避免没选中的时候，删了不应该删除的
                {
                    checkedListBox.Items.Remove(checkedListBox.SelectedItem);
                }

            }
        }

        private void today_change(ListBox listBox)
        {
            if (checkBox2.Checked)
            {
                if (listBox.SelectedItem.ToString().Replace(" ", "").Length > 0) //避免没选中的时候，删了不应该删除的
                {
                    file_NoUrgent.WriteLine(DateTime.Now.ToString("yyyy_MM_dd_") + DateTime.Now.TimeOfDay.ToString() + "[NoUrgent]$" + sstoflag(listBox.SelectedItem.ToString()));
                    listBox.Items.Remove(listBox.SelectedItem);
                }
                file_NoUrgent.Flush();
                checkBox2.Checked = false;

            }
            if (checkBox3.Checked)
            {
                if (listBox.SelectedItem.ToString().Replace(" ", "").Length > 0) //避免没选中的时候，删了不应该删除的
                {
                    listBox4.Items.Add(sstoflag(listBox.SelectedItem.ToString()));
                    listBox.Items.Remove(listBox.SelectedItem);
                }
                checkBox3.Checked = false;

            }
        }

        private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            ListBox listBox = listBox2;
            index_change(listBox);
        }

        private void modify_click(ListBox listBox)
        {
            if (richTextBox1.Text.Replace(" ", "").Length > 0)
            {
                listBox.Items.Add(sstoflag(richTextBox1.Text));
            }
            //ModfListB1 = false;
            can1in = true;
            checkBox1.Checked = false;
            richTextBox1.Text = "";
            Timekey = false;
            label7.Text = "No with doing";//标识现在开始记时的item

        }

        private void button5_Click(object sender, EventArgs e)
        {
            ListBox listBox = listBox2;
            modify_click(listBox);
        }

        private void label8_Click_1(object sender, EventArgs e)
        {

        }

        private void label9_Click(object sender, EventArgs e)
        {

        }

        private void button10_Click(object sender, EventArgs e)
        {
            string Text = this.richTextBox1.Text.Trim().ToString();
            ListBox list = this.checkedListBox3;
            string[] sArray = DateTime.Now.ToString().Split(':');

            list.Items.Add(Text + " " + sArray[0] + ":" + sArray[1]);

            richTextBox1.Clear();
            can1in = true;
            checkBox1.Checked = false;
        }

        private void button6_Click(object sender, EventArgs e)
        {
            string Text = this.richTextBox1.Text.Trim().ToString();
            ListBox list = this.checkedListBox4;
            string[] sArray = DateTime.Now.ToString().Split(':');

            list.Items.Add(Text + " " + sArray[0] + ":" + sArray[1]);

            richTextBox1.Clear();
            can1in = true;
            checkBox1.Checked = false;
        }

        private void check_click(CheckedListBox checkedListBox2)
        {
            if (checkBox1.Checked) {
                return;
            }

            foreach (string item in checkedListBox2.CheckedItems)
            {
                file_log.WriteLine(DateTime.Now.ToString("yyyy_MM_dd_") + DateTime.Now.TimeOfDay.ToString() + "[delete]$" + item.ToString());
                checkedListBox2.Items.Remove(item);
                break;
            }
            file_log.Flush();

        }

        private void checkedListBox3_SelectedIndexChanged(object sender, EventArgs e)
        {
            CheckedListBox checkedListBox = checkedListBox3;
            check_click(checkedListBox);
            index_change_check(checkedListBox);
        }

        private void checkedListBox4_SelectedIndexChanged(object sender, EventArgs e)
        {
            CheckedListBox checkedListBox = checkedListBox4;
            check_click(checkedListBox);
            index_change_check(checkedListBox);
        }

        private void button7_Click(object sender, EventArgs e)
        {
            //backup(); //放这里是为了测试
            ListBox listBox = listBox3;
            modify_click(listBox);
        }

        private void listBox3_SelectedIndexChanged(object sender, EventArgs e)
        {
            ListBox listBox = listBox3;
            index_change(listBox);//判断是非曲直（判断要不要处理，函数入口处有判断，这里只管进去就行
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void listBox4_SelectedIndexChanged(object sender, EventArgs e)
        {
            ListBox listBox = listBox4;
            index_change(listBox);//判断是非曲直（判断要不要处理，函数入口处有判断，这里只管进去就行
        }

        private void button8_Click(object sender, EventArgs e)
        {
            try
            {
                System.Diagnostics.Process.Start("foxstart-conf.txt");
            }
            catch (Exception)
            {
                
            }
            
        }
    }
}
